package com.suarte.core;

import com.suarte.utils.Persistent;
import java.math.BigDecimal;
import java.util.Date;
import org.appfuse.model.BaseObject;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Temporal;

/**
 * @date Mar 1, 2011
 * @author Ggutierrez
 */
@Entity
@Table(name = "sa_exchange_rates")
public class ExchangeRate extends BaseObject implements Persistent {

    private Long id;
    private Date date;
    private Currency fromCurrency;
    private Currency toCurrency;
    private Float rate;
    private ExchangeRateType type;

    public ExchangeRate() {
    }

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "EXCHANGE_SEQ")
    @SequenceGenerator(name = "EXCHANGE_SEQ", sequenceName = "EXCHANGE_SEQ", allocationSize = 1)
    public Long getId() {
        return id;
    }

    @Column(name = "exchange_date")
    @Temporal(javax.persistence.TemporalType.DATE)
    public Date getDate() {
        return date;
    }

    @ManyToOne
    @JoinColumn(name = "from_currency")
    public Currency getFromCurrency() {
        return fromCurrency;
    }

    @ManyToOne
    @JoinColumn(name = "to_currency")
    public Currency getToCurrency() {
        return toCurrency;
    }

    @Column(name = "rate")
    public Float getRate() {
        return rate;
    }

    @Enumerated(EnumType.STRING)
    @Column(name = "type")
    public ExchangeRateType getType() {
        return type;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public void setFromCurrency(Currency fromCurrency) {
        this.fromCurrency = fromCurrency;
    }

    public void setToCurrency(Currency toCurrency) {
        this.toCurrency = toCurrency;
    }

    public void setRate(Float rate) {
        this.rate = rate;
    }   

    public void setType(ExchangeRateType type) {
        this.type = type;
    }

    @Override
    public String toString() {
        return date != null ? date.toString()+ " -  " + rate : null;
    }

    @Override
    public boolean equals(Object other) {
        if (other == this) {
            return true;
        }
        if (!(other instanceof ExchangeRate)) {
            return false;
        }
        ExchangeRate _i = (ExchangeRate) other;
        return id.equals(_i.id);
    }

    @Override
    public int hashCode() {
        return id.hashCode();
    }
}
